package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.ExamQuestionMapper;
import com.zhijian.medical.entity.po.ExamAnswer;
import com.zhijian.medical.entity.po.ExamQuestion;
import com.zhijian.medical.entity.vo.request.ExamQuestionInsertVo;
import com.zhijian.medical.entity.vo.request.ExamQuestionSearchVo;
import com.zhijian.medical.entity.vo.response.ExamQuestionVo;
import com.zhijian.medical.service.ExamAnswerService;
import com.zhijian.medical.service.ExamQuestionService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 考核-录入题目表业务处理层
 *
 * @author HCG
 * @date 2022-01-04 15:52:37
 * @version: 1.0
 */
@Slf4j
@Service
public class ExamQuestionServiceImpl implements ExamQuestionService {

    @Autowired
    private ExamQuestionMapper mapper;

    @Autowired
    private ExamAnswerService examAnswerService;

    @Override
    public ExamQuestion selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ExamQuestionVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByWarehouseId(Long warehouseId) {
        mapper.deleteByWarehouseId(warehouseId);
    }

    @Override
    public Long insert(ExamQuestion examQuestion) {
        Date now = new Date();
        examQuestion.setCreateTime(now);
        examQuestion.setUpdateTime(now);
        return mapper.insert(examQuestion) == 1 ? examQuestion.getId() : 0L;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public Long insert(ExamQuestionInsertVo examQuestionInsertVo) {
        //先保存题目
        Long questionId = insert(ExamQuestion.builder()
                .content(examQuestionInsertVo.getContent())
                .type(examQuestionInsertVo.getType())
                .build());
        //再循环保存题目
        examQuestionInsertVo.getAnswers().forEach(examAnswerInsertVo -> examAnswerService.insert(ExamAnswer.builder()
                .content(examAnswerInsertVo.getContent())
                .questionId(questionId)
                .weatherRight(examAnswerInsertVo.getWeatherRight())
                .build()));
        return questionId;
    }

    @Override
    public boolean update(ExamQuestion examQuestion) {
        Date now = new Date();
        examQuestion.setUpdateTime(now);
        return mapper.updateByPrimaryKey(examQuestion) != 0;
    }

    @Override
    public Integer count(ExamQuestionSearchVo examQuestionSearchVo) {
        return mapper.count(examQuestionSearchVo);
    }

    @Override
    public List<ExamQuestionVo> selectList(ExamQuestionSearchVo examQuestionSearchVo) {
        return mapper.selectList(examQuestionSearchVo);
    }

    @Override
    public Page<ExamQuestionVo> selectPage(ExamQuestionSearchVo examQuestionSearchVo) {
        //先直接查总数
        int count = count(examQuestionSearchVo);
        //分页
        Page<ExamQuestionVo> page = new Page<>(true, count, examQuestionSearchVo.getPageNum(), examQuestionSearchVo.getPageSize());
        examQuestionSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ExamQuestionVo> responseVos = this.selectList(examQuestionSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public String selectIds(Long warehouseId) {
        return mapper.selectIds(warehouseId);
    }

}